package com.design.movieticket.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.design.movieticket.entity.Cinema;
import com.design.movieticket.entity.Collect;
import com.design.movieticket.entity.Movie;
import com.design.movieticket.mapper.CinemaMapper;
import com.design.movieticket.mapper.CollectMapper;
import com.design.movieticket.mapper.MovieMapper;
import com.design.movieticket.service.CinemaService;
import com.design.movieticket.service.CollectService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class CollectServiceImpl extends ServiceImpl<CollectMapper, Collect> implements CollectService {
    @Resource
    private CollectMapper collectMapper;

    @Resource
    private MovieMapper movieMapper;

    @Override
    public int collectOrNot(Collect collect) {
        if (super.getOne(new LambdaQueryWrapper<Collect>()
                .eq(Collect::getUserId,collect.getUserId())
                .eq(Collect::getMovieId,collect.getMovieId())) != null){
            return 1;
        }else{
            return 0;
        }
    }

    @Override
    public void collectMovie(Collect collect) {
        Collect tar = super.getOne(new LambdaQueryWrapper<Collect>()
                .eq(Collect::getMovieId,collect.getMovieId())
                .eq(Collect::getUserId,collect.getUserId()));
        if(tar != null){
            super.removeById(tar.getId());
        }else{
            super.saveOrUpdate(collect);
        }
    }

    @Override
    public List<Movie> seeCollectMovieList(Long userId) {
        List<Collect> collectList = super.list(new LambdaQueryWrapper<Collect>().eq(Collect::getUserId,userId));
        Long[] movieIdList = new Long[collectList.size()];
        List<Movie> movieList = new ArrayList<Movie>();
        for (int i=0; i<collectList.size(); i++){
            movieIdList[i] = collectList.get(i).getMovieId();
            movieList.add(movieMapper.selectById(movieIdList[i]));
        }
        return movieList;
    }
}
